package com.lkd.controller;


import com.lkd.entity.SkuClass;
import com.lkd.service.SkuClassService;
import com.lkd.vo.PageBean;
import com.lkd.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品类别表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-17
 */
@RestController
@RequestMapping("/api/vm-service/skuClass")
public class SkuClassController {
    @Autowired
    private SkuClassService skuClassService;

    //新增分类
    @PostMapping
    public Result save(@RequestBody SkuClass skuClass){
        boolean save = skuClassService.save(skuClass);
        return Result.success(save);
    }

    //分类搜索
    @GetMapping("/search")
    public Result search(String className,
                         @RequestParam(defaultValue = "1")Integer pageIndex,
                         @RequestParam(defaultValue = "10")Integer pageSize){
        PageBean<SkuClass>pageBean = skuClassService.search(className,pageIndex,pageSize);
        return Result.success(pageBean);
    }

    //获取分类基本信息
    @GetMapping("/{classId}")
    public Result getById(@PathVariable String classId){
        SkuClass skuClass = skuClassService.getById(classId);
        return Result.success(skuClass);
    }

    //修改分类
    @PutMapping
    public Result update(@RequestBody SkuClass skuClass){
        boolean update = skuClassService.updateById(skuClass);
        return Result.success(update);
    }

    //删除分类
    @DeleteMapping("/{classId}")
    public Result delete(@PathVariable String classId){
        Integer count=skuClassService.getSkuClassCount(Integer.valueOf(classId));
        if(count>0){
            return Result.error("该分类下有商品，无法删除");
        }else {
            boolean delete = skuClassService.removeById(classId);
            return Result.success(delete);
        }
    }
}
